//
//  HHToolBar.m
//  Flower
//
//  Created by 花花 on 2016/12/13.
//  Copyright © 2016年 花花. All rights reserved.
//

#import "HHToolBar.h"
@interface HHToolBar()
@property(nonatomic, strong) UIButton *doneBtn;
@property(nonatomic, strong) UIButton *cancelBtn;
@property(nonatomic, strong) UILabel *label;

@property(nonatomic, strong) UIView *lineView;

@property(nonatomic, copy) BtnOnClick cancelOnclick;
@property(nonatomic, copy) BtnOnClick doneOnclick;

@end
@implementation HHToolBar

- (instancetype)initWithToolbarText:(NSString *)toolBarText cancelOnClick:(BtnOnClick)cancelOnClick doneOnClick:(BtnOnClick)doneOnClick{

    if (self = [super init]) {
        
        _doneOnclick = [doneOnClick copy];
        _cancelOnclick = [cancelOnClick copy];
        self.label.text = toolBarText;
        [self addSubview:self.doneBtn];
        [self addSubview: self.cancelBtn];
        [self addSubview:self.label];
        [self addSubview:self.lineView];
    }
        
    return self;

}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    CGFloat self_width = self.bounds.size.width;
    CGFloat margin = 10;
    CGFloat btnWidth = 44;
    CGFloat lineViewHeight = 1;
    CGFloat btnOrLabelHeight = self.bounds.size.height - lineViewHeight;
    
    
    self.cancelBtn.frame = CGRectMake(margin, 0, btnWidth, btnOrLabelHeight);
    
    self.doneBtn.frame = CGRectMake(self_width - margin-_doneBtn.width, 0, btnWidth, btnOrLabelHeight);
    
    CGFloat labelX =CGRectGetMaxX(self.cancelBtn.frame)+margin;
    self.label.frame = CGRectMake(labelX, 0, self_width - labelX - 2 *margin-btnWidth, btnOrLabelHeight);
    
    self.lineView.frame = CGRectMake(0, btnOrLabelHeight, screen_width, lineViewHeight);
    
}



#pragma mark - Setter && Getter
- (UIButton *)doneBtn{

    if (!_doneBtn) {
        UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:@"完成" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
        [btn addTarget:self action:@selector(doneBtnOnClick:) forControlEvents:UIControlEventTouchUpInside];
        _doneBtn = btn;
    }

    return _doneBtn;

}
-(UIButton *)cancelBtn{

    if (!_cancelBtn) {
        UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:@"取消" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
        [btn addTarget:self action:@selector(cancelBtnOnClick:) forControlEvents:UIControlEventTouchUpInside];
        _cancelBtn = btn;
    }
    
    return _cancelBtn;
    


}

-(UILabel *)label{

    if (!_label) {
        UILabel *label = [[UILabel alloc]init];
        label.textColor =[UIColor blackColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.font =[UIFont boldSystemFontOfSize:16];
        _label = label;
    }
    
    return _label;


}

- (void)doneBtnOnClick:(UIButton *)btn{

    if (self.doneOnclick) {
        self.doneOnclick(btn);
    }
    

}
- (void)cancelBtnOnClick:(UIButton *)btn{
    
    if (self.cancelOnclick) {
        self.cancelOnclick(btn);
    }
    
}


- (UIView *)lineView{


    if (!_lineView) {
        _lineView =[[UIView alloc]init];
        _lineView.backgroundColor =[UIColor lightGrayColor];
    }
    return _lineView;

}
@end
